home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-05-01 | 11.2 KB | 469 lines | [TEXT/MPS ] |
- // UFileBasedDocument.cp
- // Copyright © 1984-1991 by Apple Computer Inc. All rights reserved.
-
- #ifndef __UFILEBASEDDOCUMENT__
- #include <UFileBasedDocument.h>
- #endif
-
- #ifndef __GEOMETRY__
- #include <Geometry.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __STANDARDFILE__
- #include <StandardFile.h>
- #endif
-
- #ifndef __ULIST__
- #include <UList.h>
- #endif
-
- #ifndef __UFILE__
- #include <UFile.h>
- #endif
-
- #ifndef __UVIEW__
- #include <UView.h>
- #endif
-
- #ifndef __UFILEHANDLER__
- #include <UFileHandler.h>
- #endif
-
- #ifndef __UPRINTHANDLER__
- #include <UPrintHandler.h>
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include <UMacAppUtilities.h>
- #endif
-
- #ifndef __UERRORMGR__
- #include <UErrorMgr.h>
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include <UMacAppGlobals.h>
- #endif
-
- #ifndef __UMEMORY__
- #include <UMemory.h>
- #endif
-
- #ifndef __UITERATOR__
- #include <UIterator.h>
- #endif
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- pascal void TFileBasedDocument::Initialize(void)// override
- {
- inherited::Initialize();
-
- fFileHandler = NULL;
- fScrapType = ' '; // need constant kNoScrapType
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- pascal void TFileBasedDocument::IFileBasedDocument(TFile* itsFile,
- const OSType itsFileType,
- const OSType itsCreator,
- const OSType itsScrapType,
- Boolean usesDataFork,
- Boolean usesRsrcFork,
- Boolean keepsDataOpen,
- Boolean keepsRsrcOpen)
- {
- this->IDocument();
-
- fFileHandler = this->DoMakeFileHandler(itsFile, itsFileType, itsCreator, usesDataFork, usesRsrcFork, keepsDataOpen, keepsRsrcOpen);
- fScrapType = itsScrapType;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- pascal TFileHandler* TFileBasedDocument::DoMakeFileHandler(TFile* itsFile,
- const OSType itsFileType,
- const OSType itsCreator,
- Boolean usesDataFork,
- Boolean usesRsrcFork,
- Boolean keepsDataOpen,
- Boolean keepsRsrcOpen)
- {
- TFileHandler * aFileHandler;
-
- aFileHandler = new TFileHandler;
- aFileHandler->IFileHandler(this, itsFile, itsFileType, itsCreator, usesDataFork, usesRsrcFork, keepsDataOpen, keepsRsrcOpen);
- return aFileHandler;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAClose
-
- pascal void TFileBasedDocument::Free(void) // override
- {
- fFileHandler = (TFileHandler *)FreeIfObject(fFileHandler);
-
- inherited::Free();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFile
-
- pascal Boolean TFileBasedDocument::AlreadyOpen(TFile* aFile)// override
- {
- return fFileHandler->FileAlreadyOpen(aFile);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFile
-
- pascal void TFileBasedDocument::CheckFile(short rsrcId,
- short rsrcIndex,
- Boolean reverting)
- {
- OSErr err;
- Str255 name;
- Str255 s;
-
- err = fFileHandler->FileChanged(reverting); // don't care about the file type if saving
- if (err == errFileChanged)
- {
- name = fTitle;
- GetIndString(s, rsrcId, rsrcIndex);
- ParamText(name, s, "", "");
- if (MacAppAlert(phFileChanged, NULL) == cancel)// !!! This should be programatically defeatable
- Failure(noErr, msgCancelled);
- }
- else if ((err != noErr) && reverting)
- Failure(err, 0);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- pascal void TFileBasedDocument::DoNeedDiskSpace(TFile* ,
- long& dataForkBytes,
- long& rsrcForkBytes)
- {
- if (fSavePrintInfo)
- {
- if (fFileHandler->HasRsrcFork())
- rsrcForkBytes = rsrcForkBytes + GetHandleSize(fPrintInfo);// don't assume size
- else
- dataForkBytes = dataForkBytes + kPrintInfoSize;
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- pascal void TFileBasedDocument::DoRead(TFile* aFile,
- Boolean forPrinting)
- {
- if (fSavePrintInfo)
- this->DoReadPrintInfo(aFile, forPrinting);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- pascal void TFileBasedDocument::DoReadPrintInfo(TFile* aFile,
- Boolean)
- {
- Handle hPrintInfo;
-
- if (fFileHandler->FileExists())
- {
- if (aFile->IsRsrcForkOpen())
- {
- hPrintInfo = GetResource(kPrintInfoRsrcType, kPrintInfoRsrcID);// Read the print info resource
- FailNILResource(hPrintInfo);
- if (!fPrintInfo)
- {
- DetachResource(hPrintInfo);
- fPrintInfo = hPrintInfo;
- }
- else
- {
- BlockMove(*hPrintInfo, *fPrintInfo, GetHandleSize(fPrintInfo));
- ReleaseResource(hPrintInfo);
- }
- }
- else
- {
- long count = kPrintInfoSize;
- SignedByte savedState;
-
- if (!fPrintInfo)
- fPrintInfo = NewPermHandle(kPrintInfoSize);
-
- savedState = LockHandleHigh(fPrintInfo);
- FailOSErr(aFile->ReadData((*fPrintInfo), count));
- HSetState(fPrintInfo, savedState);
- }
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- pascal void TFileBasedDocument::AboutToSaveFile(CmdNumber itsCmd,
- Boolean& makingCopy)
- {
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- pascal void TFileBasedDocument::DoWrite(TFile* aFile,
- Boolean makingCopy)
- {
- if (fSavePrintInfo)
- this->DoWritePrintInfo(aFile, makingCopy);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- pascal void TFileBasedDocument::DoWritePrintInfo(TFile* aFile,
- Boolean)
- {
- long count;
-
- if (fPrintInfo == NULL)
- {
- #if qDebug
- ProgramBreak("no print record in document");
- #endif
-
- }
- else
- {
- Handle tempHandle;
-
- if (aFile->IsRsrcForkOpen())
- {
- tempHandle = fPrintInfo;
- FailOSErr(HandToHand(tempHandle));
- AddResource(tempHandle, kPrintInfoRsrcType, kPrintInfoRsrcID, "");
- FailResError();
- }
- else
- {
- count = kPrintInfoSize;
- FailOSErr(aFile->WriteData(*fPrintInfo, count));
- }
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- pascal void TFileBasedDocument::CloseFile(void)
- {
- fFileHandler->CloseFile();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- pascal void TFileBasedDocument::ReadDocument(Boolean forPrinting)// override
- {
- Str63 fileName;
-
- fFileHandler->ReadFile(forPrinting);
- fFileHandler->GetFileName(fileName);
- fTitle = fileName;
- this->SetChangeCount(0);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- pascal void TFileBasedDocument::RevertDocument(void)
- {
- FailInfo fi;
-
- if (fi.Try())
- {
- TCommand * lastCommand;
-
- this->CheckFile(kIDBuzzString, bzRevertAnyways, TRUE);
-
- lastCommand = this->GetLastCommand();
- if ((lastCommand != NULL) && (lastCommand->fChangedObject == this))
- this->CommitLastCommand();
-
- this->FreeData();
-
- if (fFileHandler->FileExists())
- fFileHandler->ReadFile(kForDisplay);
- else
- {
- CObjectIterator iter(fViewList);
-
- for (TView* view = (TView*) iter.FirstObject(); iter.More(); view = (TView*) iter.NextObject())
- if (view->fPrintHandler)
- view->fPrintHandler->Reset();
-
- this->DoInitialState();
- }
-
- this->SetChangeCount(0);
- fi.Success();
- }
- else // Recover
- {
- if (fi.error == fnfErr)
- fi.error = errRevertFNF;
- if (fi.message == 0)
- gErrorParm3 = fTitle;
- FailNewMessage(fi.error, fi.message, msgRevertFailed);
- fi.ReSignal();
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- pascal void TFileBasedDocument::SaveDocument(CmdNumber itsCmdNumber)// override
- {
- TCommand * lastCommand;
- Boolean askForFilename =!fFileHandler->FileExists() || ((itsCmdNumber != cSave) && (itsCmdNumber != cClose));
- Boolean makingCopy = itsCmdNumber == cSaveCopy;
- Boolean copyFInfo =!(askForFilename || makingCopy);
-
- if (copyFInfo)
- this->CheckFile(kIDBuzzString, bzSaveAnyways, FALSE);
-
- lastCommand = this->GetLastCommand();
- if (fCommitOnSave || (!makingCopy) && (lastCommand != NULL) && (lastCommand->fChangedObject == this))
- this->CommitLastCommand();
-
- fFileHandler->SaveFile(itsCmdNumber, askForFilename, copyFInfo, makingCopy);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- pascal void TFileBasedDocument::FileHasBeenSaved(const Str255& newName)
- {
- this->SetChangeCount(0);
-
- if (fTitle != newName)
- this->SetTitle(newName);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- pascal void TFileBasedDocument::SFPutParms(CmdNumber itsCmdNumber,
- Str255& prompt,
- Str255& ,
- short& dlgID,
- Point& where,
- ProcPtr& dlgHook,
- ProcPtr& modalFilter,
- Ptr& activeList,
- ProcPtr& activateProc,
- Ptr& yourDataPtr)
- {
- short idx;
-
- if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
- {
- dlgID = sfPutDialogID;
- SetPt(where, -1, -1);
- }
- else
- {
- DialogTHndl dlogTemplate;
- Rect dialogRect;
-
- dlgID = putDlgID; // putDlgID defined by Standard File
- dlogTemplate = (DialogTHndl) GetResource('DLOG', dlgID);
- if (dlogTemplate)
- {
- dialogRect = (*dlogTemplate)->boundsRect;
- CenterRectOnScreen(dialogRect, TRUE, TRUE, TRUE);
- where = dialogRect[topLeft];
- }
- else
- SetPt(where, 100, 100);
- }
-
- switch (itsCmdNumber)
- {
- case cSave:
- case cSaveAs:
- idx = bzSaveAs;
- break;
-
- case cSaveCopy:
- idx = bzSaveCopy;
- break;
-
- default:
- idx = 0;
- break;
- }
-
- if (idx == 0)
- prompt = "";
- else
- GetIndString(prompt, kIDBuzzString, idx);
-
- dlgHook = NULL;
- modalFilter = NULL;
- activeList = NULL;
- activateProc = NULL;
- yourDataPtr = NULL;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- pascal TFileHandler* TFileBasedDocument::GetFileHandler(void)
- {
- return fFileHandler;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- pascal void TFileBasedDocument::SetTitle(const Str255& aTitle)
- {
- inherited::SetTitle(aTitle);
-
- fFileHandler->SetFileName(aTitle);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFields
-
- pascal void TFileBasedDocument::Fields(TObject* obj)
- {
- obj->DoToField("TFileBasedDocument", (Ptr)NULL, bClass);
- obj->DoToField("fFileHandler", (Ptr) & fFileHandler, bObject);
-
- inherited::Fields(obj);
- }
-
-
-